//
//  UWeChat.m
//  UPaySDK
//
//  Created by xu yao on 2021/3/4.
//

#import "UWeChat.h"
#import "EnvironmentDefine.h"
#import "UPayReq.h"
#import "USDKConnector.h"
#import "UWXApi.h"
#import "HCToast.h"
#import "UPaySDK.h"
@interface UWeChat () <UWXApiDelegate>

@property (nonatomic, copy) NSString *wechatAppid;
@property (nonatomic, copy) NSString *universalLink;
@property (nonatomic, copy) USDKPayResultHandler payResultHandle;

@end

static UWeChat *currentHelper = nil;
@implementation UWeChat

+ (void)registerWeChat:(NSString *)appid universalLink:(NSString *)universalLink {
//    if (![WXApi isWXAppInstalled]) {
//        LCLog(@"微信未安装");
//        return;
//    }
//    [[currentHelper.wxApiClass class] openWXApp];
    [[currentHelper.wxApiClass class] startLogByLevel:UWXLogLevelNormal logBlock:^(NSString *log) {
        NSLog(@"log : %@", log);
    }];
    UWXApi *wxApi = [[[currentHelper.wxApiClass class] alloc] init];
    NSLog(@"%@", [[wxApi class] getApiVersion]);
    BOOL isSuccess = [[currentHelper.wxApiClass class] registerApp:appid universalLink:universalLink];
    if (isSuccess) {
        LCLog(@"微信注册成功");
    }
}

+ (void)weChatPayWithPayReq:(UPayReq *)payReq {
//    if (![WXApi isWXAppInstalled]) {
//        LCLog(@"微信未安装");
//        return;
//    }
    // 判断用户是否注册sdk
//    PayReq *request = [[PayReq alloc] init];
    
    Class baseReq = NSClassFromString(@"PayReq");
    if (!baseReq) {
        return;
    }
    id request = [[baseReq alloc] init];
    [request setValue:payReq.partnerId forKey:@"partnerId"];
    [request setValue:payReq.prepayId forKey:@"prepayId"];
    [request setValue:payReq.package forKey:@"package"];
    [request setValue:payReq.nonceStr forKey:@"nonceStr"];
    [request setValue:@(payReq.timeStamp) forKey:@"timeStamp"];
    [request setValue:payReq.sign forKey:@"sign"];
    if (request) {
        UWXApi *wxApi = [currentHelper.wxApiClass class];
        [[wxApi class] sendReq:request completion:^(BOOL success) {

        }];
        
    }
}

+ (void)handleOpenURL:(NSURL *)url onPayResult:(USDKPayResultHandler)payResultHandle {
    currentHelper.payResultHandle = payResultHandle;
    [[currentHelper.wxApiClass class] handleOpenURL:url delegate:[self currentHelper]];
}

- (void)onResp:(UBaseResp *)resp {
    NSString *message = resp.errStr;
    if (resp.errCode == UWXSuccess) {
        message = @"支付成功";
    } else {
        message = @"支付失败";
    }
    [[HCToast shareInstance] showToast:message];
    if (self.payResultHandle) {
        self.payResultHandle(USDKPlatformTypeWeChat, resp.errCode == 0 ? UPaySuccess : UPayFail, message);
    }
    if (resp.errCode == UWXSuccess) {
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            [UPaySDK closecheckstand];
        });
    }
}

+ (instancetype)currentHelper {
    if (currentHelper == nil)
        currentHelper = [[UWeChat alloc] init];
    return currentHelper;
}

- (instancetype)init {
    if (self = [super init]) {
        
    }
    return self;
}


@end

